<?php
class EventLogic{
	static $table = "event";
	const ADD_FOOD_EVENT = 0;
	const REMARK_RECIPE_EVENT = 1;
	const FAV_RECIPE_EVENT = 2;
	const EDIT_RECIPE_EVENT = 3;
	
	static $eventMap = array(
		self::ADD_FOOD_EVENT=>"doneNum",
		self::REMARK_RECIPE_EVENT=>"remarkNum",
		self::FAV_RECIPE_EVENT=>"favNum",
	);
	
	/**
	 * 得到最近的活动
	 *
	 * @param unknown_type $num
	 * @return unknown
	 */
	static function getLastEvents($num=2) {
		$table = self::$table;	
		$sql = "select type_id,recipe_id,target_id,actor_id,create_time from ".$table." where private=0 order by event_id desc limit ".$num;
		$db = DBMgr::getSlaver($table);
		$rs = $db->query($sql);
		$ret = array();
		while($rs->hasNext()) {
			$row = $rs->next();
			$actorId = $row["actor_id"];
			require_once 'Xiachufang/entity/Actor.php';
			$actor = new ActorEntity();
			if (!$actor->get($actorId)) continue;
			$ret[] = array(
				"actorId"=>$row["actor_id"],
				"actorName"=>$actor->nickname,
				"desc"=>self::getEventDesc($row["type_id"],$row["recipe_id"],$row["target_id"]),
				"createTime"=>TimeUtil::format($row["create_time"]),
			);
		}
		return $ret;
	}
	/**
	 * 获取活动的描述
	 *
	 * @param unknown_type $typeId
	 * @param unknown_type $targetId
	 * @return unknown
	 */
	private static function getEventDesc($typeId, $recipeId, $targetId) {
		require_once 'Xiachufang/entity/Recipe.php';
		$desc = '';
		switch($typeId) {
			case self::ADD_FOOD_EVENT:
				require_once 'Xiachufang/entity/Food.php';
				$food = new FoodEntity();
				if (!$food->get($targetId)) break;
				$desc .= '做了一道 <a href="/food/'.$food->foodId.'">'.$food->name.'</a>';
				break;
			case self::REMARK_RECIPE_EVENT:
				require_once 'Xiachufang/entity/Remark.php';
				$remark = new RemarkEntity();
				if (!$remark->get($targetId)) break;
				
				$recipe = new RecipeEntity();
				if (!$recipe->get($remark->recipeId)) break;
				$desc .= '在 <a href="/recipe/'.$recipe->recipeId.'">'.$recipe->name.
					'</a> 里评论：“<a href="/recipe/'.$recipe->recipeId.'#remark'.$remark->remarkId.'">'.$remark->content.'</a>”';
				break;
			case self::FAV_RECIPE_EVENT:
				require_once 'Xiachufang/entity/Favorite.php';
				$recipe = new RecipeEntity();
				if (!$recipe->get($recipeId)) break;
				$desc .= '收藏了 <a href="/recipe/'.$recipe->recipeId.'">'.$recipe->name.'</a>';
				break;
			case self::EDIT_RECIPE_EVENT:
				$recipe = new RecipeEntity();
				if (!$recipe->get($recipeId)) break;
				$desc .= '编辑了 <a href="/recipe/'.$recipe->recipeId.'">'.$recipe->name.'</a>';
				break;
		}
		return $desc;
	}

	/**
	 * 得到用户对某个菜谱的动作
	 * @param unknown_type $recipeId
	 */
	static function getActorEvents($recipeId, $actorId) {
		$ret = array(
			"haveFaved"=>false,
			"haveDone"=>false,
			"haveEdited"=>false,
			"haveRemarked"=>false,
		);
		if (!$actorId) return $ret;
		
		$table = self::$table;
		$sql = "select type_id,target_id from ".$table." where recipe_id=".$recipeId." and actor_id=".$actorId;
		$db = DBMgr::getSlaver($table);
		$events = $db->query($sql)->asArray();
		foreach($events as $event) {
			$typeId = $event["type_id"];
			$targetId = $event["target_id"];
			switch($typeId) {
				case self::ADD_FOOD_EVENT:
					$ret["haveDone"] = $targetId;
					break;
				case self::FAV_RECIPE_EVENT:
					$ret["haveFaved"] = true;
					break;
				case self::EDIT_RECIPE_EVENT:
					$ret["haveEdited"] = $targetId;
					break;
				case self::REMARK_RECIPE_EVENT:
					$ret["haveRemarked"] = $targetId;
					break;
			}
		}
		return $ret;
	}
	
	/**
	 * 增加事件
	 *
	 * @param unknown_type $typeId
	 * @param unknown_type $recipeId
	 * @param unknown_type $targetId
	 * @param unknown_type $actorId
	 * @param unknown_type $createTime
	 * @param unknown_type $private
	 * @return unknown
	 */
	static function addEvent($typeId, $recipeId, $targetId,$actorId, $createTime, $private=false) {
		$table = self::$table;
		$sql = "insert ignore into ".$table."(type_id,recipe_id,target_id,actor_id,create_time,private) values(?,?,?,?,?,?)";
		$db = DBMgr::getSlaver($table);
		$ret = $db->execute($sql, array($typeId, $recipeId, $targetId, $actorId, $createTime, $private ? 1 : 0))->success;
		
		$num = self::$eventMap[$typeId];
		if ($num) {
			require_once 'Xiachufang/entity/Recipe.php';
			$recipe = new RecipeEntity();
			if ($recipe->get($recipeId)) {
				$recipe->$num++;
				$recipe->update();
			}
		}
		return $ret;
	}
	
	/**
	 * 修改事件的隐私属性
	 *
	 * @param unknown_type $typeId
	 * @param unknown_type $recipeId
	 * @param unknown_type $targetId
	 * @param unknown_type $actorId
	 * @param unknown_type $private
	 */
	static function modifyEventPrivate($typeId, $recipeId, $targetId,$actorId, $private) {
		$table = self::$table;
		$sql = "update ".$table." set private=? where type_id=? and recipe_id=? and target_id=? and actor_id=?";
		$db = DBMgr::getSlaver($table);
		$db->execute($sql, array($private ? 1 : 0, $typeId, $recipeId, $targetId, $actorId));
	}
	
	/**
	 * 删除事件
	 *
	 * @param unknown_type $typeId
	 * @param unknown_type $recipeId
	 * @param unknown_type $targetId
	 * @return unknown
	 */
	static function deleteEvent($typeId, $recipeId, $targetId, $actorId) {
		$table = self::$table;
		$sql = "delete from ".$table." where type_id=? and recipe_id=? and target_id=? and actor_id=?";
		$db = DBMgr::getSlaver($table);
		$ret = $db->execute($sql, array($typeId, $recipeId, $targetId, $actorId))->success;
		
		$num = self::$eventMap[$typeId];
		if ($num) {
			require_once 'Xiachufang/entity/Recipe.php';
			$recipe = new RecipeEntity();
			if ($recipe->get($recipeId)) {
				if ($recipe->$num > 0) $recipe->$num--;
				$recipe->update();
			}
		}
		return $ret;
	}

	/**
	 * 分页获取用户的收藏
	 *
	 * @param unknown_type $actorId
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @return PageResult
	 */
	static function pagedGetActorFavs($actorId, $page, $pageSize) {
		$table = self::$table;
		$sql = "select target_id from ".$table." where actor_id=".$actorId." and type_id=".self::FAV_RECIPE_EVENT." order by event_id desc";
		$db = DBMgr::getSlaver($table);
		return $db->fullPagedQuery($sql, $page, $pageSize);
	}
}
?>